﻿using Code;
using github.heerheer.card2.UI.Pages;
using github.heerheer.card2.UI.ViewModels;
using HeerCards;
using Microsoft.Win32;
using Panuon.UI.Silver;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace github.heerheer.card2.UI.Views
{
    /// <summary>
    /// CardEditorView.xaml 的交互逻辑
    /// </summary>
    public partial class CardEditorView : Window
    {
        public CardEditorViewModel CardEditorViewModel { get; set; }

        public CardEditorView(string pool, string levelName)
        {
            InitializeComponent();
            this.DataContext = CardEditorViewModel = new CardEditorViewModel();
            CardEditorViewModel.LoadLevelContent(pool, levelName);
        }

        private void Btn_AddCard(object sender, RoutedEventArgs e)
        {
            var nc = new HCCard() { Description = "", Name = "Blank_Card" };

            CardEditorViewModel.Level.addCard(nc);
            CardList.ItemsSource = null;
            CardList.ItemsSource = CardEditorViewModel.Level.Cards;
        }

        private void Btn_CloseAndSave(object sender, RoutedEventArgs e)
        {
            App.SavePool(CardEditorViewModel.Pool);
            //MessageBoxX.Show("返回到卡池详情，并会将卡片的数据同步到卡池详情\n请在返回后进行保存", "关闭等级编辑");
            DialogResult = true;
        }

        private void CardEditBtn_Click(object sender, RoutedEventArgs e)
        {
            var btntag = int.Parse(((Button)sender).Tag.ToString());
            var c = CardEditorViewModel.Pool.GetCardWithGuid(btntag);
            CardSettings cs = new CardSettings(c);
            var result = cs.ShowDialog();
            if (result == true)
            {
                var card2 = cs.Card;
                //卡池做出了更改
                CardEditorViewModel.Pool.ReplaceCardWithGuid(btntag, new HCCard()
                {
                    Name = card2.Name,
                    Description = card2.Description,
                    ExtraProbability = card2.ExtraProbability,
                    LocalImagePath = card2.LocalImagePath
                });
            }
            this.DataContext = CardEditorViewModel;
        }

        private void CardDeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            var btntag = int.Parse(((Button)sender).Tag.ToString());
            var r = MessageBoxX.Show("确定要删除这张卡?", "删除", this, MessageBoxButton.YesNo);
            if (r == MessageBoxResult.Yes)
            {
                CardEditorViewModel.Pool.RemoveCardWithGuid(btntag);
            }
            this.DataContext = CardEditorViewModel;
            CardList.ItemsSource = null;
            CardList.ItemsSource = CardEditorViewModel.Level.Cards;
        }

        private void SavePool()
        {
        }

        private void Btn_QuickImport(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "选择需要导入成卡牌的图片";
            var path = AppMain.Common.AppDirectory.Replace("app\\github.heerheer.card2", "image\\card2");
            fd.InitialDirectory = path;
            fd.Multiselect = true;
            var r = fd.ShowDialog();
            if (r == true)
            {
                foreach (var item in fd.FileNames)
                {
                    var imagepath = item.Replace(path, "");
                    var name = Path.GetFileNameWithoutExtension(item);
                    var desc = "";

                    var nc = new HCCard()
                    {
                        Name = name,
                        Description = desc,
                        ExtraProbability = 1,
                        LocalImagePath = imagepath
                    };
                    CardEditorViewModel.Level.addCard(nc);
                }

                CardList.ItemsSource = null;
                CardList.ItemsSource = CardEditorViewModel.Level.Cards;
                MessageBox.Show("导入完成", "快速构建");
            }
        }
    }
}